﻿using Prism.Commands;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DemountCIM.Windows;
using static DemountCIM.MainWindowModel;
using DemountCIM.Framework;
using Def = CommonLibrary.WFFile.SysDef;
using DemountCIM.Framework.DataBase.Table;
using DemountCIM.Framework.DataBase;
using DemountCIM.View;
using DemountCIM.Framework.SECSGEM;
using System.Timers;
using DeMountCIM.Windows;
using CommonLibrary.WFFile;
using WpfUIDLL.MessageBox;

namespace DemountCIM
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainWindowModel model;
        System.Timers.Timer dataUpdateTimer;
        KingEQP EQP;

        _eCommState tempOldCommState;
        _eControlState tempOldControlState;
        public MainWindow()
        {
            ChangeLanguageService.ChangeLanguagePublish(Def.GetCurLanguage());

            InitializeComponent();

            MachineCtrl.GetInstance().Initialize();

            var list = new List<Naviagtion>
            {
                new Naviagtion(1,"主页面",new OverView(),(Geometry)Application.Current.Resources["icon_home"]),
                new Naviagtion(2,"设备调试",new DebugView(), (Geometry) Application.Current.Resources["icon_debug"]),
                new Naviagtion(3,"MES调试",new MesView(), (Geometry) Application.Current.Resources["icon_mes"]),
            };

            model = new MainWindowModel(list);
            this.DataContext = model;
            model.NowPage = list[0].Content;


            EQP = KingEQP.GetInstance();

            dataUpdateTimer = new System.Timers.Timer();
            dataUpdateTimer.Interval = (double)(1000);  //ms
            dataUpdateTimer.Elapsed += new ElapsedEventHandler(tmr_timeout);
            dataUpdateTimer.Enabled = true;
            dataUpdateTimer.AutoReset = true;      //false:只定时一次, true：重复执行
            dataUpdateTimer.Start();

        }

        void tmr_timeout(object sender, ElapsedEventArgs e)
        {
            var state = EQP.GetCommState();
            if (tempOldCommState != state)
            {
                model.SetCommState(state);
                tempOldCommState = state;
            }

            var controlState = EQP.GetControlState();
            if (tempOldControlState != controlState)
            {
                model.SetControlState(controlState);
                tempOldControlState = controlState;
            }



        }


        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            Int16 id = Convert.ToInt16((sender as RadioButton).Tag);
            model.NowPage = model.PageList.FirstOrDefault(a => a.Id == id).Content;
        }



        #region 用户

        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {
            var userRepository = DataBaseContainer.GetInstance().GetUserRepository();
            TbUser user = new TbUser();
            if (!userRepository.GetCurUser(ref user) || string.IsNullOrEmpty(user.Name))
            {
                List<TbUser> userList = new List<TbUser>();
                var userWindow = new LoginWindow();
                userWindow.ShowDialog();
                TbUser user1 = new TbUser();
                if (userRepository.GetCurUser(ref user1) && !string.IsNullOrEmpty(user1.Name))
                {
                    model.User.UserName = user1.Name;
                    model.User.UserLevel = user1.Level.ToString();
                    model.User.LoginState = "已登录";
                }
            }
        }

        private void userBtn_Click(object sender, RoutedEventArgs e)
        {
            var userRepository = DataBaseContainer.GetInstance().GetUserRepository();

            TbUser user = new TbUser();
            userRepository.GetCurUser(ref user);
            if (!string.IsNullOrEmpty(user.Name) && user.Level <= UserLevel.管理员)
            {
                new UserManagerWindow().ShowDialog();
            }
            else if (!string.IsNullOrEmpty(user.Name))
            {
                PwdModifyWindow window = new PwdModifyWindow();
                window.SetUserInfo(user);

                window.ShowDialog();
            }
        }

        private void loginOutBtn_Click(object sender, RoutedEventArgs e)
        {
            DataBaseContainer.GetInstance().GetUserRepository().UserLogout();
            model.User.Clear();
        }

        #endregion

        private void settingBtn_Click(object sender, RoutedEventArgs e)
        {
            TbUser user = new TbUser();
            DataBaseContainer.GetInstance().GetUserRepository().GetCurUser(ref user);
            if (!string.IsNullOrEmpty(user.Name))
            {
                OptionWindow window = new OptionWindow();

                window.ShowDialog();
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void mesLoginBtn_Click(object sender, RoutedEventArgs e)
        {
            MesLoginWindow mesLoginWindow = new MesLoginWindow();

            mesLoginWindow.ShowDialog();
        }

        private void CN_Click(object sender, RoutedEventArgs e)
        {
            ChangeLanguageService.ChangeLanguage(SysDef.LanguagePack.Chinese);
            ShowMsgBox.ShowDialog("重启软件后生效！", MessageType.MsgMessage);
        }

        private void EG_Click(object sender, RoutedEventArgs e)
        {
            ChangeLanguageService.ChangeLanguage(SysDef.LanguagePack.English);
            ShowMsgBox.ShowDialog("Take effect after restarting the software！", MessageType.MsgMessage);
        }
    }
}